# Maintainer: Philippe Weyland <philippe.weyland@libertysurf.fr>

_realname=gmic
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.5.4
pkgrel=1
pkgdesc="A Full-Featured Open-Source Framework for Image Processing (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gmic.eu"
msys2_repository_url='https://github.com/GreycLab/gmic'
msys2_documentation_url='https://gmic.eu/reference/'
license=('spdx:CECILL-2.1 AND CECILL-C')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-opencv"
         "${MINGW_PACKAGE_PREFIX}-openexr")
# "https://github.com/GreycLab/gmic/releases/download/v.${pkgver}/gmic_${pkgver}.tar.gz"
source=("https://gmic.eu/files/source/gmic_${pkgver}.tar.gz"
        001-building-man-pages.patch)
sha256sums=('7e3a1aa75c00603fb01fa2cad686c8d2f1ec81dd820f6688cba3c44dd255aeef'
            'e7e0ca90fb431708cea2127c686f5f8146792082e3ea3c2cdcaa1e43d45a195c')

prepare() {
  cd ${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/001-building-man-pages.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_LIB=ON \
    -DBUILD_LIB_STATIC=OFF \
    -DBUILD_BASH_COMPLETION=OFF \
    -DENABLE_FFMPEG=ON \
    -DENABLE_FFTW=ON \
    -DENABLE_GRAPHICSMAGICK=ON \
    -DENABLE_JPEG=ON \
    -DENABLE_OPENCV=ON \
    -DENABLE_OPENEXR=ON \
    -DENABLE_OPENMP=ON \
    -DENABLE_PNG=ON \
    -DENABLE_TIFF=ON \
    -DENABLE_ZLIB=ON \
    -DENABLE_DYNAMIC_LINKING=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
